/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 *
 */

import fse from 'fs-extra';
import os from 'os';
import path from 'path';

import { HVIGOR_USER_HOME_DIR_NAME } from '../const/const.js';

/**
 * 获取user home下的.hvigor目录，
 *
 */
export function getHvigorUserHomeCacheDir(): string {
  const defaultUserHome = path.resolve(os.homedir(), HVIGOR_USER_HOME_DIR_NAME);
  const hvigorUserHome = process.env.HVIGOR_USER_HOME;

  if (hvigorUserHome === undefined || !path.isAbsolute(hvigorUserHome)) {
    return defaultUserHome;
  }
  fse.ensureDirSync(hvigorUserHome);
  return hvigorUserHome;
}
